import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入描述的路径:");
        String s = scanner.next();
        File file = new File(s);
        //判断文件目录是否存在
        if(!file.exists()) {
            System.out.println("路径文件不存在!");
            return;
        }

        System.out.println("请输入要删除的文件名称:");
        String s1 = scanner.next();
        toDelete(file,s1);
    }

    public static void toDelete(File file,String s) {
        File[] files = file.listFiles();
        //判断文件目录下是否为空
        if(files == null) {
            return;
        }

        for (File f : files) {
            if(f.isDirectory()) {
                toDelete(f,s);
            }else {
                tryDelete(f,s);
            }
        }
    }

    public static void tryDelete(File file,String s) {
        if(file.getName().contains(s)) {
            try {
                System.out.println("是否要删除" + file.getCanonicalFile());
                Scanner sca = new Scanner(System.in);
                System.out.println("确认删除:Y/N");
                String s1 = sca.next();
                if(s1.equals("Y")) {
                    file.delete();
                    System.out.println("删除成功");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
